package com.ss.android.article.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.design.a;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;

import java.util.ArrayList;

public class SSTabHost extends TabHost implements TabHost.OnTabChangeListener {
    class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

        public DummyTabFactory(Context arg1) {

            this.mContext = arg1;
        }

        public View createTabContent(String arg4) {
            View v0 = new View(this.mContext);
            v0.setMinimumWidth(0);
            v0.setMinimumHeight(0);
            return v0;
        }
    }

    public interface OnLayoutChangeListener {
        void onLayoutChanged(int arg1);
    }

    static class SavedState extends View.BaseSavedState {
        public static final Parcelable.Creator CREATOR;
        String curTab;

        static {
            CREATOR = new Creator<SavedState>() {
                public final SavedState createFromParcel(Parcel arg3) {
                    return new SSTabHost.SavedState(arg3);
                }

                public final SavedState[] newArray(int arg2) {
                    return new SavedState[arg2];
                }
            };
        }

        SavedState(Parcelable arg1) {
            super(arg1);
        }

        private SavedState(Parcel arg2) {
            super(arg2);
            this.curTab = arg2.readString();
        }

        public String toString() {
            return "FragmentTabHost.SavedState{" + Integer.toHexString(System.identityHashCode(this)) + " curTab=" + this.curTab + "}";
        }

        public void writeToParcel(Parcel arg2, int arg3) {
            super.writeToParcel(arg2, arg3);
            arg2.writeString(this.curTab);
        }
    }

    static final class TabInfo {
        private final Bundle args;
        private final Class clss;
        private Fragment fragment;
        private final String tag;
        private final boolean use_view;
        private final View view;

        TabInfo(String arg3, View arg4) {

            this.use_view = true;
            this.tag = arg3;
            this.clss = null;
            this.args = null;
            this.view = arg4;
        }

        TabInfo(String arg2, Class arg3, Bundle arg4) {

            this.use_view = false;
            this.tag = arg2;
            this.clss = arg3;
            this.args = arg4;
            this.view = null;
        }
    }

    private boolean mAttached;
    private int mContainerId;
    private Context mContext;
    private FragmentManager mFragmentManager;
    private int mLastLayoutBottom;
    private TabInfo mLastTab;
    private OnLayoutChangeListener mLayoutChangeListener;
    private TabHost.OnTabChangeListener mOnTabChangeListener;
    private FrameLayout mRealTabContent;
    private final ArrayList mTabs;

    public SSTabHost(Context arg3) {
        super(arg3, null);
        this.mTabs = new ArrayList();
        this.initFragmentTabHost(arg3, null);
    }

    public SSTabHost(Context arg2, AttributeSet arg3) {
        super(arg2, arg3);
        this.mTabs = new ArrayList();
        this.initFragmentTabHost(arg2, arg3);
    }

    public void addTab(TabHost.TabSpec arg5, View arg6, boolean arg7) {
        arg5.setContent(new DummyTabFactory(this.mContext));
        TabInfo v1 = new TabInfo(arg5.getTag(), arg6);
        FrameLayout.LayoutParams v2 = this.generateDefaultLayoutParams();
        ViewGroup v0 = this.getRealTabContent();
        v2.topMargin = ((View) v0).getPaddingTop();
        if (arg7) {
            v2.bottomMargin = ((View) v0).getPaddingBottom();
        }

        int v0_1 = UIUtils.b(((View) v0));
        if (v0_1 < 0 || v0_1 >= this.getChildCount()) {
            v0_1 = -1;
        }

        this.addView(arg6, v0_1, ((ViewGroup.LayoutParams) v2));
        this.mTabs.add(v1);
        this.addTab(arg5);
    }

    public void addTab(TabHost.TabSpec arg4, Class arg5, Bundle arg6) {
        arg4.setContent(new DummyTabFactory(this.mContext));
        String v0 = arg4.getTag();
        TabInfo v1 = new TabInfo(v0, arg5, arg6);
        if (this.mAttached) {
            v1.fragment = this.mFragmentManager.findFragmentByTag(v0);
            if (v1.fragment != null && !v1.fragment.isDetached()) {
                FragmentTransaction v0_1 = this.mFragmentManager.beginTransaction();
                v0_1.detach(v1.fragment);
                v0_1.commitAllowingStateLoss();
            }
        }

        this.mTabs.add(v1);
        this.addTab(arg4);
    }

    public void dispatchWindowFocusChanged(boolean arg4) {
        View v0_2;
        super.dispatchWindowFocusChanged(arg4);
        int v0 = this.getCurrentTab();
        if (v0 >= 0 && v0 < this.mTabs.size()) {
            Object v0_1 = this.mTabs.get(v0);
            if (v0_1 != null) {
                View v1 = null;
                if (((TabInfo) v0_1).use_view) {
                    v0_2 = ((TabInfo) v0_1).view;
                } else {
                    if (((TabInfo) v0_1).fragment != null && ((TabInfo) v0_1).fragment.isVisible()) {
                        v0_2 = ((TabInfo) v0_1).fragment.getView();
                    } else {
                        v0_2 = v1;
                    }
                }

                if (v0_2 == null) {
                    return;
                }

                if (v0_2.getVisibility() != View.VISIBLE) {
                    return;
                }

                v0_2.dispatchWindowFocusChanged(arg4);
            }
        }
    }

    private FragmentTransaction doTabChanged(String arg6, FragmentTransaction arg7) {
        Object v1 = null;
        int v2 = 0;
        while (v2 < this.mTabs.size()) {
            Object v0 = this.mTabs.get(v2);
            if (!((TabInfo) v0).tag.equals(arg6)) {
                v0 = v1;
            }

            ++v2;
            v1 = v0;
        }

        if (v1 == null) {
            throw new IllegalStateException("No tab known for tag " + arg6);
        }

        String v2_1 = "MainTabHost";
        StringBuilder v4 = new StringBuilder("doTabChanged ").append(arg6).append(" ");
        String v0_1 = this.mLastTab != null ? this.mLastTab.tag : "--";
        Logger.d(v2_1, v4.append(v0_1).toString());
        if (this.mLastTab != v1) {
            if (arg7 == null) {
                arg7 = this.mFragmentManager.beginTransaction();
            }

            if (this.mLastTab != null) {
                if (this.mLastTab.use_view) {
                    this.mLastTab.view.setVisibility(View.INVISIBLE);
                } else if (this.mLastTab.fragment != null) {
                    arg7.hide(this.mLastTab.fragment);
                }
            }

            if (v1 != null) {
                if (((TabInfo) v1).use_view) {
                    ((TabInfo) v1).view.setVisibility(View.VISIBLE);
                } else if (((TabInfo) v1).fragment == null) {
                    ((TabInfo) v1).fragment = Fragment.instantiate(this.mContext, ((TabInfo) v1).clss.getName(), ((TabInfo) v1).args);
                    arg7.add(this.mContainerId, ((TabInfo) v1).fragment, ((TabInfo) v1).tag);
                } else {
                    if (((TabInfo) v1).fragment.isDetached()) {
                        arg7.attach(((TabInfo) v1).fragment);
                    }

                    if (!((TabInfo) v1).fragment.isHidden()) {
                    } else {
                        arg7.show(((TabInfo) v1).fragment);
                    }
                }
            }

            this.mLastTab = ((TabInfo) v1);
        }

        return arg7;
    }

    private void ensureContent() {
        if (this.mRealTabContent == null) {
            this.mRealTabContent = this.findViewById(this.mContainerId);
            if (this.mRealTabContent == null) {
                throw new IllegalStateException("No tab content FrameLayout found for id " + this.mContainerId);
            }
        }
    }

    private void ensureHierarchy(Context arg8) {
        int v2 = android.R.id.tabs;
        int v4 = -1;
        if (this.findViewById(v2) == null) {
            LinearLayout v0 = new LinearLayout(arg8);
            v0.setOrientation(LinearLayout.VERTICAL);
            this.addView(((View) v0), new FrameLayout.LayoutParams(v4, v4));
            TabWidget v1 = new TabWidget(arg8);
            v1.setId(v2);
            v1.setOrientation(LinearLayout.HORIZONTAL);
            v0.addView(((View) v1), new LinearLayout.LayoutParams(v4, -2, 0f));
            FrameLayout v1_1 = new FrameLayout(arg8);
            v1_1.setId(android.R.id.tabcontent);
            v0.addView(((View) v1_1), new LinearLayout.LayoutParams(0, 0, 0f));
            v1_1 = new FrameLayout(arg8);
            this.mRealTabContent = v1_1;
            this.mRealTabContent.setId(this.mContainerId);
            v0.addView(((View) v1_1), new LinearLayout.LayoutParams(v4, 0, 1f));
        }
    }

    public Fragment getFragment(int arg2) {
        Fragment v0 = arg2 < 0 || arg2 >= this.mTabs.size() ? null : ((TabInfo) this.mTabs.get(arg2)).fragment;
        return v0;
    }

    public ViewGroup getRealTabContent() {
        return this.mRealTabContent;
    }

    private void initFragmentTabHost(Context arg4, AttributeSet arg5) {
        TypedArray v0 = arg4.obtainStyledAttributes(arg5, new int[]{16842995}, 0, 0);
        this.mContainerId = v0.getResourceId(0, 0);
        v0.recycle();
        super.setOnTabChangedListener(((TabHost.OnTabChangeListener) this));
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        String v3 = this.getCurrentTabTag();
        FragmentTransaction v1 = null;
        int v2;
        for (v2 = 0; v2 < this.mTabs.size(); ++v2) {
            Object v0 = this.mTabs.get(v2);
            if (!((TabInfo) v0).use_view) {
                ((TabInfo) v0).fragment = this.mFragmentManager.findFragmentByTag(((TabInfo) v0).tag);
                if (((TabInfo) v0).fragment != null && !((TabInfo) v0).fragment.isDetached()) {
                    if (((TabInfo) v0).tag.equals(v3)) {
                        this.mLastTab = ((TabInfo) v0);
                    } else {
                        if (v1 == null) {
                            v1 = this.mFragmentManager.beginTransaction();
                        }

                        v1.detach(((TabInfo) v0).fragment);
                    }
                }
            } else if (((TabInfo) v0).tag.equals(v3)) {
                this.mLastTab = ((TabInfo) v0);
            } else {
                ((TabInfo) v0).view.setVisibility(View.INVISIBLE);
            }
        }

        this.mAttached = true;
        if (v3 != null) {
            FragmentTransaction v0_1 = this.doTabChanged(v3, v1);
            if (v0_1 == null) {
                return;
            }

            try {
                v0_1.commitAllowingStateLoss();
                this.mFragmentManager.executePendingTransactions();
            } catch (IllegalStateException v0_2) {
                v0_2.printStackTrace();
            }
        }
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.mAttached = false;
    }

    protected void onLayout(boolean arg3, int arg4, int arg5, int arg6, int arg7) {
        super.onLayout(arg3, arg4, arg5, arg6, arg7);
        if ((arg3) && this.mLayoutChangeListener != null) {
            this.mLayoutChangeListener.onLayoutChanged(arg7 - this.mLastLayoutBottom);
        }

        this.mLastLayoutBottom = arg7;
    }

    protected void onRestoreInstanceState(Parcelable arg2) {
        super.onRestoreInstanceState(((SavedState) arg2).getSuperState());
        this.setCurrentTabByTag(((SavedState) arg2).curTab);
    }

    protected Parcelable onSaveInstanceState() {
        SavedState v1 = new SavedState(super.onSaveInstanceState());
        v1.curTab = this.getCurrentTabTag();
        return ((Parcelable) v1);
    }

    public void onTabChanged(String arg2) {
        if (this.mAttached) {
            FragmentTransaction v0 = this.doTabChanged(arg2, null);
            if (v0 == null) {
            } else {
                try {
                    v0.commitAllowingStateLoss();
                    this.mFragmentManager.executePendingTransactions();
                } catch (IllegalStateException v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }

        label_8:
        if (this.mOnTabChangeListener != null) {
            this.mOnTabChangeListener.onTabChanged(arg2);
        }
    }

    public void setOnLayoutChangeListener(OnLayoutChangeListener arg1) {
        this.mLayoutChangeListener = arg1;
    }

    public void setOnTabChangedListener(TabHost.OnTabChangeListener arg1) {
        this.mOnTabChangeListener = arg1;
    }

    @Deprecated
    public void setup() {
        throw new IllegalStateException("Must call setup() that takes a Context and FragmentManager");
    }

    public void setup(Context arg1, FragmentManager arg2) {
        this.ensureHierarchy(arg1);
        super.setup();
        this.mContext = arg1;
        this.mFragmentManager = arg2;
        this.ensureContent();
    }

    public void setup(Context arg3, FragmentManager arg4, int arg5) {
        this.ensureHierarchy(arg3);
        super.setup();
        this.mContext = arg3;
        this.mFragmentManager = arg4;
        this.mContainerId = arg5;
        this.ensureContent();
        this.mRealTabContent.setId(arg5);
        if (this.getId() == -1) {
            this.setId(android.R.id.tabhost);
        }
    }

    public boolean tryReplaceTab(int arg4, String arg5, Class arg6, Bundle arg7) {
        boolean v0_1;
        if (arg4 == this.getCurrentTab() || arg4 < 0 || arg4 >= this.mTabs.size()) {
            v0_1 = false;
        } else {
            if (!a.isTextEmpty(arg5) && arg6 != null) {
                Object v0 = this.mTabs.get(arg4);
                if (v0 != null && ((TabInfo) v0).fragment == null && (!((TabInfo) v0).use_view || ((TabInfo) v0).view == null)) {
                    this.mTabs.set(arg4, new TabInfo(arg5, arg6, arg7));
                    v0_1 = true;
                    return v0_1;
                }

                return false;
            }

            v0_1 = false;
        }

        return v0_1;
    }
}

